package com.tanhua.app.Service;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tanhua.app.exception.BusinessException;
import com.tanhua.app.interceptor.UserHolder;
import com.tanhua.commons.utils.Constants;
import com.tanhua.config.template.HuanXinTemplate;
import com.tanhua.dubbo.api.FriendApi;
import com.tanhua.dubbo.api.UserApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.dubbo.api.UserLikeApi;
import com.tanhua.model.pojo.User;
import com.tanhua.model.pojo.UserInfo;
import com.tanhua.model.vo.ContactVo;
import com.tanhua.model.vo.ErrorResult;
import com.tanhua.model.vo.PageResult;
import com.tanhua.model.vo.UserInfoVo;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class MessagesService {
    @DubboReference
    private UserApi userApi;
    @DubboReference
    private UserInfoApi userInfoApi;
    @Autowired
    private HuanXinTemplate huanXinTemplate;
    @DubboReference
    private FriendApi friendApi;
    @DubboReference
    private UserLikeApi userLikeApi;

    //    环信ID查询用户信息
    public UserInfoVo findUserInfoByHuanxin(String huanxinId) {
        User user = userApi.findByHuanxin(huanxinId);
        UserInfo userInfo = userInfoApi.findById(user.getId());
        UserInfoVo vo = UserInfoVo.init(userInfo);
        return vo;
    }

    //    联系人添加
    public void contacts(Long friendId) {
        Long userId = UserHolder.getUserId();
        Boolean aBoolean = huanXinTemplate.addContact(Constants.HX_USER_PREFIX + friendId, Constants.HX_USER_PREFIX + userId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.contactsError());
        }
        friendApi.save(friendId,userId);
    }

    //     联系人删除
    public void removeContacts(Long friendId){
        Long userId = UserHolder.getUserId();
        Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + friendId, Constants.HX_USER_PREFIX + userId);
        if (!aBoolean) {
            throw new BusinessException(ErrorResult.removeContactsError());
        }
        friendApi.delete(friendId,userId);
    }

    //    联系人列表
    public PageResult findFriends(Integer page, Integer pagesize, String keyword) {
        Long userId = UserHolder.getUserId();
        List<Long> friendIds = friendApi.findById(userId);
        if(CollUtil.isEmpty(friendIds)){
            return new PageResult();
        }
        Page<UserInfo> pages = userInfoApi.findMapByIds(friendIds,page,pagesize,keyword);
        List<ContactVo> vos = pages.getRecords().stream().map(userInfo -> ContactVo.init(userInfo)).collect(Collectors.toList());
        return new PageResult(page,pagesize, (int) pages.getTotal(),vos);
    }

    //    视频用户关注
    public void userFocus(Long focusId) {
        Long userId = UserHolder.getUserId();
        if(userId==focusId){
            throw new BusinessException(ErrorResult.addContactsError());
        }
        if(userLikeApi.saveOrUpdate(focusId,userId,true)){
            huanXinTemplate.addContact(userId.toString(),focusId.toString());
        }
    }

    //    视频用户关注 - 取消
    public void userUnFocus(Long friendId) {
        Long userId = UserHolder.getUserId();
        if(!userLikeApi.saveOrUpdate(friendId,userId,false)){
            Boolean aBoolean = huanXinTemplate.deleteContact(Constants.HX_USER_PREFIX + friendId, Constants.HX_USER_PREFIX + userId);
            if (!aBoolean) {
                throw new BusinessException(ErrorResult.removeContactsError());
            }
        }
    }
}
